#include "client.h"

int func_puts_client(int netfd, char * filename)
{
    printf("文件不存在!\n");                                              
    //printf("i will send filename\n");
    train_t pTrain;
    int ret = access(filename, F_OK);
    //printf("filename=%s\n",filename);
    filename[strcspn(filename, "\n")] = '\0';
    if (ret == -1) {
        printf("文件不存在!\n");                                              
        return -1;
    }
    pTrain.length =strlen(filename);
    //printf("train filenamt-------------- len is %d\n",pTrain.length);
    //printf("train filename data is %s\n",pTrain.data);
    //memcpy(pTrain.data, filename, strlen(filename));///给traindata赋初值
    //send(netfd, pTrain, sizeof(pTrain.), 0);
    printf("train filenamt-------------- len is %d\n",pTrain.length);
    send(netfd,&pTrain.length, sizeof(pTrain.length), MSG_NOSIGNAL);
    memcpy(pTrain.data, filename, strlen(filename));///给traindata赋初值
    printf("train filename data is %s\n",pTrain.data);
    send(netfd, pTrain.data, pTrain.length, MSG_NOSIGNAL);
    //printf("文件名字的长度和文件名字本身我已经发过去了\n");
    int fd = open(filename, O_RDWR);
    if (fd == -1) {
        printf("open file is failed!\n");
        return -1;
    }
    while(1) {
        bzero(pTrain.data, sizeof(pTrain.data));
        ssize_t sret =read(fd, pTrain.data, sizeof(pTrain.data));
        pTrain.length = sret;
        send(netfd, &pTrain.length, sizeof(pTrain.length), MSG_NOSIGNAL);
        send(netfd, pTrain.data, pTrain.length, MSG_NOSIGNAL);
        if(sret == 0) {
            break;
        }

    }
    close(fd);
    return 0;
}

